<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>菜单管理</title>
    <link rel="stylesheet" href="/static/admin/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="/static/admin/css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-container">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                <button class="layui-btn layui-btn-warm" id="add">添加菜单</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<script type="text/html" id="icon">
    <span class="{{d.icon}}"></span>
</script>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="/static/admin/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/admin/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 2,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#munu-table',
            url: '/api/system/permission',
            page: false,
            cols: [[
                {field: 'id', title: 'ID', width: 70, fixed: true},
                {field: 'icon', align: 'center', title: '图标', width: 60, templet: '#icon'},
                {field: 'title', minWidth: 200, title: '权限名称'},
                {field: 'method', title: '请求方式', width: 100},
                {field: 'href', title: '路由', width: 500},
                {field: 'target', title: '跳转类型'},
                {
                    field: 'status', title: '状态', templet: function (d) {
                        if (d.status === 1) {
                            return '<span style="color: green">使用中</span>';
                        } else {
                            return '<span style="color: red">已禁用</span>';
                        }
                    }
                },
                {field: 'sort', width: 80, align: 'center', title: '排序号'},
                {
                    field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                        if (d.is_menu === 1) {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        }
                        if (d.pid === 0) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge layui-bg-green">菜单</span>';
                        }
                    }, title: '类型'
                },
                {fixed: 'right', width: 120, align: 'center', title: '操作', templet: '#auth-state'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        $("#add").click(function () {
            layer.open({
                type: 2,
                title: "添加菜单",
                content: "/admin/system/menu_edit",
                area: ['800px', '700px'],
                maxmin: true
            });
        })

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm('确定删除吗?', function (index) {
                    loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.ajax({
                        url: "/api/system/menu",
                        type: "DELETE",
                        data: {id: data.id},
                        success: function (res) {
                            layer.close(loading);
                            layer.close(index);
                            if (res.code === 0) {
                                layer.msg(res.msg, {time: 1000, icon: 1});
                                obj.del();
                            } else {
                                layer.msg(res.msg, {time: 1000, icon: 2});
                            }
                        },
                        error: function (res) {
                            layer.msg(res.msg);
                            return false
                        }
                    })
                })
            } else if (layEvent === 'edit') {
                layer.open({
                    type: 2,
                    title: "编辑菜单",
                    content: "/admin/system/menu_edit?id=" + data.id,
                    area: ['800px', '700px'],
                    maxmin: true
                });
            }
        });
    });
</script>
</body>
</html>